//
//  BandCardStore.m
//  NewPro
//
//  Created by RYX on 2018/10/25.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "BandCardStore.h"
#import "BankCarList.h"
#import "BandCardModel.h"

@implementation BandCardStore

#pragma mark - 银行卡列表
- (void)getBandCardListSuccess:(void(^)(NSArray *listArr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/getMoney/card",IP];
    
    [HttpTool getUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            NSArray *arr = [BandCardModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"items"]];
            success(arr);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}

#pragma mark - 添加银行卡获取验证码
- (void)getCodeWithTelephoneNumber:(NSString *)telephoneNumber Success:(void(^)(NSString *code))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/getAuthentication",IP];
    
    NSDictionary *dic = @{
                          @"telephoneNumber":telephoneNumber
                          };
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
        
            success(responseObject[@"data"][@"code"]);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}


#pragma mark - 添加银行卡
- (void)addBandCardListWithCustomId:(NSString *)customId banNum:(NSString *)bandNum bankName:(NSString *)bankName andPhone:(NSString *)phone andType:(NSString *)type Success:(void(^)(void))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/getMoney/addcar",IP];
    
    NSDictionary *dic = @{
                          @"customId":customId,
                          @"cardNumber":bandNum,
                          @"bankName":bankName,
                          @"phone":phone,
                          @"type":type,
                          };
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}


#pragma mark - 删除银行卡
- (void)deleteBankCardWithId:(NSString *)bankCardId Success:(void(^)(void))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/getMoney/card/%@",IP,bankCardId];
    
    [HttpTool deleteUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}

#pragma mark - 设置默认银行卡
- (void)setDefaultBankCardWithId:(NSString *)bankCardId Success:(void(^)(void))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/card/default/%@",IP,bankCardId];
    
    [HttpTool putUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}
@end
